//
//  BudgetTotalSetView.swift
//  KoiAccounting
//  月总预算设置
//  Created by 纪洪文 on 16.04.25.
//

import SwiftUI

struct BudgetSetCategoryView: View {
    
    var selectedCategory: AccountingCategory
    
    var selectedBudget: AccountingBudget? = nil
    
    @State var reset = false
    
    @State var inputAmount = "0.00"
    
    var onDoneClick: (String) -> Void
    
    var body: some View {
        
        VStack {
            VStack (alignment:.leading){
                HStack {
                    Text("设置")
                        .font(.system(size: 14))
                        .foregroundColor(Color.koiColorTitleSecond)
                    Text(selectedCategory.name.orEmpty)
                        .font(.system(size: 14))
                        .foregroundColor(Color.koiColorPrimary)
                    Text("月预算：")
                        .font(.system(size: 14))
                        .foregroundColor(Color.koiColorTitleSecond)
                }
                HStack {
                    TextField("请输入预算金额", text: $inputAmount)
                        .font(.system(size: 34))
                        .fontWeight(.medium)
                        .foregroundColor(Color.koiColorPrimary)
                    Spacer()
                    Button(action: {
                        onDoneClick("0")
                    }) {
                        Text("清除预算")
                            .font(.system(size: 13))
                            .foregroundColor(Color.koiColorPrimary)
                            .padding(EdgeInsets(top: 8, leading: 16, bottom: 8, trailing: 16))
                            .background(Color.koiColorPrimary.opacity(0.04))
                            .cornerRadius(16)
                    }
                }
            }
            .padding(EdgeInsets(top: 10, leading: 20, bottom: 10, trailing: 20))
            .cornerRadius(16)
            
            NumericKeypadView(
                numericClickCallback: { inputText in
                    inputAmount = inputText
                },
                doneClickCallback: {
                    onDoneClick(inputAmount)
                },
                reset: $reset
            )
            .frame(height: 264)
            .padding(16)
            .background(Color.koiColorBackgroundItem)
            .cornerRadius(16)
        }
        // 去除边距
        .ignoresSafeArea(.container, edges: .bottom)
        .onAppear {
            if let budget = selectedBudget {
                inputAmount = budget.amount?.toAmountTwoDecimalPlaces() ?? inputAmount
            }
        }
    }
}

#Preview {
    BudgetSetCategoryView(
        selectedCategory: AccountingCategory(),
        onDoneClick: { _ in })
}
